﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.BLL;
using PBOffice.DAL;

/// <summary>
///TreeViewManager 的摘要说明
/// </summary>
public class TreeViewManager
{
    OrganManager organManager = new OrganManager();
    Department dep = new Department();
    DepartmentManager depManager = new DepartmentManager();   
	public void tv_Load(TreeView tv)
	{       
        List<Organ> organList = organManager.ShowAllOrganInfo();        //机构信息集合
        foreach (Organ org in organList)
        {
            TreeNode tn = new TreeNode();
            tn.Text = org.OrganShortName;                                    //节点显示文本：机构简称
            tn.Value = org.OrganNo.ToString();                               //节点的隐藏文本：机构编号
            tv.Nodes.Add(tn);                                           //添加节点到树上
        }
	}
    /// <summary>
    /// 选中节点变化事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void tv_SelectedNodeChanged(TreeView tv)
    {
        List<v_DepOrgan> depList = new List<v_DepOrgan>();
        TreeNodeExpandORCollapse(tv);          //节点的展开或折叠   
        tv.SelectedNode.ChildNodes.Clear();  //清空选定节点的子节点    
   
        if (tv.SelectedNode.Parent != null)     //非一级节点：部门节点
        {
            int depNo = int.Parse(tv.SelectedValue.ToString());     //选中部门编号
            depList = depManager.GetSubDepartments(depNo);       //根据上级部门编号获取下级部门信息
            if (depList.Count>0)
            {
                AddTreeNode(depList,tv);                                   //为选中节点添加子节点：部门信息 
            }
        }
        else                                       //一级节点：机构节点
        {
            int organNo = int.Parse(tv.SelectedValue.ToString());       //选中机构编号
            depList = depManager.ShowDepInfoByOrganNo(organNo);         //根据机构编号获取部门信息
            if (depList.Count>0)
            {
                AddTreeNode(depList, tv);                                       //为选中节点添加子节点：部门信息 
            }
        }
    }
    /// <summary>
    /// 展开选中节点，其他节点闭合
    /// </summary>
    /// <param name="tv"></param>
    private static void TreeNodeExpandORCollapse(TreeView tv)
    {
        if (tv.SelectedNode.Parent != null)       //选中节点非根节点时
        {
            TreeNodeCollection tc = tv.SelectedNode.Parent.ChildNodes;     //选中节点父节点的子节点集合
            foreach (TreeNode tn in tc)
            {
                if (tn != tv.SelectedNode)          //节点为兄弟节点时
                {
                    tn.CollapseAll();               //节点折叠
                }
            }
        }
        else                                     //选中节点为跟节点时
        {
            tv.CollapseAll();            //关闭树上所有结点
        }
    }
    //为选中节点添加子节点
    private void AddTreeNode(List<v_DepOrgan> depList, TreeView tv)
    {
        foreach (v_DepOrgan dep in depList)
        {
            TreeNode tn = new TreeNode();
            tn.Text = dep.DepId;
            tn.Value = dep.DepNo.ToString();
            tv.SelectedNode.ChildNodes.Add(tn);                 //为选中节点添加子节点
        }
        tv.SelectedNode.Expand();         //展开选中节点 
    }
}